(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open UTypes
open Logging
open Dim
open Substitute
open FontMetric
open Box
open Page
  
(* an area of the page *)
type area = {
  ar_name     : uc_string;
  ar_shape    : area_shape;
  ar_contents : area_contents_function
}

and page_update = (page * area_finaliser * page_state)

(* function to fill the area with its contents *)
and area_contents_function = page -> area_shape -> (extended_glyph_item list) list -> page_state -> page_update option

(* description of a page     *)
and page_layout = {
  pl_width : num;                       (* paper dimensions *) 
  pl_height : num;
  pl_areas : area array                 (* the areas *)
}

and area_finaliser = page -> page

(* state while filling areas *)
and page_state = {
  ps_page_no : int;
  ps_old_marks : uc_string DynUCTrie.t; (* marks in previous pages *)
  ps_new_marks : (uc_string * uc_string) list; (* marks in the current page *)
  ps_galleys : ((box list) * Galley.galley) DynUCTrie.t;
  ps_layouts : page_layout DynUCTrie.t;
  ps_next_layout : page_layout;         (* layout of the next page *)
  ps_new_floats : floating list; (* floats found in the current page, in reverse order *)
  ps_badness : num; (* the accumulated badness of the current layout *)
  ps_finished : bool;      (* the galley filling some area is empty *)
}

(* |page_run_state| contains the information needed about previous pages *)
type page_run_state

val empty_page_layout : page_layout
val tracing_page_layout : bool ref
val simple_page_update : page -> page_state -> page_update option
val get_page_info : page -> page_state -> page_info
val new_page_run_state : int -> num -> Galley.galley DynUCTrie.t -> page_layout DynUCTrie.t -> page_run_state
val page_no : page_run_state -> int
val get_galley_table : page_run_state -> Galley.galley DynUCTrie.t
val layout_one_sided : page_layout -> int -> page_layout
val layout_two_sided : page_layout -> page_layout -> int -> page_layout
val layout_run_of_pages : (int -> page_layout) -> (page_run_state -> bool) -> page_run_state -> ((FontMetric.page list) * page_run_state)

(*value abort_on_empty_area  : int -> page_run_state -> bool; *)
val abort_when_done : page_run_state -> bool

val abort_on_page : int -> page_run_state -> bool
